<template>
	<view class="mainbgc mainpadding">
		<header-top :messageInfo="{typeFlag:true,message:'添加商品',liner:false}">
			<view class="rightmsg" slot="rightmsg">
				<view class="rightmsg_content">
					<view class="nostrongtext" @click="surebtn">确定</view>
				</view>
			</view>
		</header-top>
		<!-- 销内容 -->
		<view class="mainpadding ffffff margin_top">
			<view class="xiaolan" style="font-size: 32rpx; font-weight: bold;">{{dataAll.name}}</view>
			<view class="flexright">
				<u-icon name="minus-circle-fill" color="#ff0000" size="28" @click="reducefn"></u-icon>
				<input type="number" style="text-align: center;width: 100rpx;" v-model="number" @blur="blurfn">
				<u-icon name="plus-circle-fill" color="#0BBDCB" size="28" @click="addfn"></u-icon>
			</view>
			
			<view v-for="(item,index) in dataAll.activitygoods">
				<view class="flexbetween xiabiank margin_top">
					<view class="titletext">{{item.goods.name}}</view>
					<!-- 大单位 -->
					<view class="xiaolan" v-show="item.join_goods_unit==1">{{item.price}}/{{item.goods.biggoodsunit.name}}</view>
					<!-- 中单位 -->
					<view class="xiaolan" v-show="item.join_goods_unit==2">{{item.price}}/{{item.goods.middlegoodsunit.name}}</view>
					<!-- 小单位 -->
					<view class="xiaolan" v-show="item.join_goods_unit==3">{{item.price}}/{{item.goods.smallgoodsunit.name}}</view>
				</view>
				<view class="flexbuju margin_top" v-show="item.producedate_status==1" @click="getProductTime(item,index)">
					<view class="huisetext">生产日期：</view>
					<input type="text" class="inputs xiabiank" style="text-align: center;" placeholder="选择生产日期" disabled="" v-model="item.produce_time">
					<view class="sanjiaox"></view>
				</view>
				<view class="flexbuju margin_top" v-show="item.producedate_status==1">
					<view class="huisetext">保质期：</view>
					<input type="number" class="inputs xiabiank" style="text-align: center;" placeholder="保质期天数" disabled="" v-model="item.goods.bzq">
					<view class="huisetext">天</view>
				</view>
				<view class="flexbuju margin_top">
					<view class="huisetext">数量：</view>
					<input type="number" class="inputs xiabiank" disabled style="text-align: center;" placeholder="输入数量" @blur="inputBulrNum(item)" v-model="item.enternum">
				</view>
				<!-- <view class="flexbetween margin_top">
					<view class="smalltext" style="color: #2cd551;">可用库存：100件</view>
					<view class="smalltext">单位换算：1瓶=100件</view>
				</view> -->
			</view>
		</view>
		<u-picker :show="timePickershow" :columns="timePickercolumns" @confirm="timePickersure" title="选择生产日期"
			keyName="produce_time_text" @cancel="timePickershow=false"></u-picker>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				timePickershow: false,
				timePickercolumns: [],
				getmsg:{
					type:3,//3销售单
					warehouse_id:"",
					id:"",
				},
				dataAll:{},
				selectindex:"",
				selectvalue:"",
				number:1,
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.getmsg));
			this.getmsg = data
			this.getdetail()
		},
		methods:{
			reducefn(){
				if(this.number!=1){
					this.number = this.number-1
					this.computednum()
				}
			},
			addfn(){
				this.number = this.number+1
				this.computednum()
			},
			blurfn(){
				this.number = this.number>1?Math.round(this.number):1
				this.computednum()
			},
			computednum(){
				this.dataAll.activitygoods.forEach(item=>{
					item.enternum = this.number * item.num
				})
			},
			getdetail(){
				httpRequest.request('/dhtapi/Saleorder/activityDetail', 'GET', {
					id: this.getmsg.id, 
				}).then(res => {
					res.data.activitygoods.forEach(item=>{
						this.$set(item,"produce_time","")
						this.$set(item,"enternum",item.num)
					})
					this.dataAll = res.data
				})
			},
			inputBulrNum(item){
				if(item.num>item.enternum){
					httpRequest.toast("输入数量不能小于活动数量!")
					item.enternum = item.num
					return false
				}
				if(item.enternum % item.num !=0){
					httpRequest.toast("输入数量须为活动数量的倍数!")
					item.enternum = item.num
					return false
				}
			},
			// 获取生产日期
			getProductTime(item, index) {
				this.selectindex = index
				this.selectvalue = item.join_goods_unit
				uni.showLoading({
					mask: true,
					title: "请稍候。。。"
				})
				httpRequest.request('/dhtapi/Saleorder/getGoodsProduceTime', 'GET', {
					goods_id: item.goods_id,
					warehouse_id: this.getmsg.warehouse_id,
					difference_type: item.join_goods_unit
				}, false, false, true).then(res => {
					uni.hideLoading()
					this.timePickercolumns = []
					if (res.data.length) {
						this.timePickercolumns.push(res.data.reverse())
						this.timePickershow = true
					} else {
						httpRequest.toast("当前仓库无此类商品！")
					}
				}).catch(err => {})
			},
			timePickersure(e){
				let obj = {
					"1": "biggoodsunit",
					"2": "middlegoodsunit",
					"3": "smallgoodsunit"
				}
				this.dataAll.activitygoods[this.selectindex].goods[obj[this.selectvalue]].produce_time = e.value[0].produce_time_text
				this.timePickershow = false
			},
			surebtn(){
				// if((Number(this.dataAll.num) - Number(this.dataAll.buy_num))<this.number){
				// 	httpRequest.toast("剩余数量不足！")
				// 	return
				// }
				let arr = []
				let obj = {
					"1": "biggoodsunit",
					"2": "middlegoodsunit",
					"3": "smallgoodsunit"
				}
				this.dataAll.activitygoods.forEach(item=>{
					arr.push({
						goods_id: item.goods_id,
						price: item.price,
						num: item.enternum,
						produce_time: item.produce_time,
						goods_sku_id: item.goods[obj[item.join_goods_unit]].id,
						goods_sku_name: item.goods[obj[item.join_goods_unit]].name,
						notes: "",
						goodsinfo: item.goods,
						type: 1,
						goods_type: item.goods_type==1?2:3,
						send_num: 0, //判断当前是否是退货商品，如果为退货商品赠送数量为0
						send_notes:"",
					})
				})
				if (this.getmsg.type == 3) { //销售单
					let xiaoshoudanInfo = JSON.parse(uni.getStorageSync("xiaoshoudanInfo"))
					let concatarr = xiaoshoudanInfo.data.concat(arr)
					xiaoshoudanInfo.data = xiaoshoudanInfo.data.concat(arr)
					uni.setStorageSync('xiaoshoudanInfo', JSON.stringify(xiaoshoudanInfo))
				}
				httpRequest.toast("添加成功")
				setTimeout(()=>{
					uni.navigateBack({
						delta:3
					})
				},500)
			},
		}
		
	}
</script>

<style lang="scss" scoped>
	.mainbgc{
		background-color: #FAFAFA;
		    min-height: calc(100vh - 100rpx);

	}
	.xiaohuitext{
		font-size: 22rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}
	.textcontent {
		text-align: center;
	}

	.nostrongtext {
		padding-bottom: 20rpx;
	}

	.allbtn {
		border-radius: 43rpx;
		padding: 10rpx 50rpx;
		border: 2rpx solid #E7E7E7;
	}

	.lansebtn {
		border-radius: 0 43rpx 43rpx 0;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #0BBDCB;
	}

	.shanchu {
		border-radius: 43rpx 0 0 43rpx;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
		border-right: none;
	}

	.smalltext {
		font-size: 22rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.sanjiaox {
		border: 14rpx solid transparent;
		border-top: 14rpx solid #666666;
	}

	.inputsl {
		width: 25%;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		text-align: center;
	}

	.flexbuju {
		display: flex;
		align-items: flex-start;
	}

	.inputs {
		width: 40%;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.huisetext {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.icon {
		background-image: url('@/static/image/system/xiao.png');
		width: 108rpx;
		height: 52rpx;
		margin-right: 34rpx;
		background-size: 100% 100%;
	}

	.icontow {
		background-image: url('@/static/image/system/tui.png');
		width: 108rpx;
		height: 52rpx;
		margin-right: 34rpx;
		background-size: 100% 100%;
	}

	.jiahao {
		width: 43.33rpx;
		height: 43.33rpx;
		background-image: url('@/static/image/system/jiahao.png');
		background-size: 100% 100%;
	}

	.xiabiank {
		padding-bottom: 20rpx;
		border-bottom: 2rpx solid rgba(0, 0, 0, 0.03);
	}
</style>
